from django.db import models
from django.utils.safestring import mark_safe


def add_model_chrome(target, request):
	""" Add chrome abilities to a model object for easier html """

	user = request.user
	object = target


	def __chrome_header():
		""" Return a nice list of allowable header links for the given object """
		update = ""

		if object.permission(user,'update'):
			update = ("""<a class="bd-header-link" href="%s">update</a>"""	% object.get_update_url())

		if object.permission(user,'delete'):
			update = (update + """ | <a class="bd-header-link" href="%s">delete</a>"""
						% object.get_delete_url())

		return mark_safe('<h1>%s %s</h1>' % (object.__unicode__(), update))

	object.chrome_header = __chrome_header


	# Return updated object for use in forms.
	return object


def model_chrome_list_header(title, object_class, user):
	"""
	Return a nice list of header links for a list of objects belonging to a user
	"""
	if not object_class.insert_permission(user) :
		return mark_safe('<h1>%s</h1>' % title)
	else:
		return mark_safe("""<h1>%s <a class="bd-header-link" href="%s">insert</a></h1>"""
						% (title, object_class.get_insert_url() ) )


def child_model_chrome_list_header(title, object_class, user, owner):
	"""
	Return a nice list of header links for a list of objects with a master object.
	"""
	if not object_class.insert_permission(user,owner) :
		return mark_safe('<h1>%s</h1>' % title)
	else:
		return mark_safe("""<h1>%s <a class="bd-header-link" href="%s">insert</a></h1>"""
						% (title, object_class.get_insert_url(owner) ) )
